# 函数式编程
l1 = list(map(str, range(10)))  # 与[str(i) for i in range(10)]等价
l2 = [str(i) for i in range(10)]  # 推荐使用列表推导式
print(l1, l2)  # ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

# filter map reduce

# 元素过滤
# 找过数字字母串
seq = ['foo', '12', '***', 'a32', '12!!']


# filter函数方式
def isAlnum(x):
    return x.isalnum()


l = list(filter(isAlnum, seq))
print(l)  # ['foo', '12', 'a32']

# 列表推导方式
l = [x for x in seq if x.isalnum()]
print(l)  # ['foo', '12', 'a32']

# lambda方式
l = list(filter(lambda x: x.isalnum(), seq))
print(l)  # ['foo', '12', 'a32']

# reduce 函数
numbers = [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33]
from functools import reduce

# 求和
total = reduce(lambda x, y: x + y, numbers)
print(f"total={total}")  # total=1161
# 最小值
mi = reduce(lambda x, y: x if x < y else y, numbers)
print(f"min={mi}")  # min=32
# 最大值
ma = reduce(lambda x, y: x if x > y else y, numbers)
print(f"max={ma}")  # max=119

#

# map
# 不会立刻调用，需要外部调用，比如用list包裹起来
print(list(map(lambda x: x * 2, numbers)))


def doubleX(x):
    print('x=', x)
    return x * 2


print(list(map(doubleX, numbers)))  # [144, 202, 216, 216, 222, 88, 64, 238, 222, 228, 216, 200, 66]

# apply
